<template>
    <view class="container">
        <view class="btn-ads" style="width:750rpx;height:100rpx;background-color: #555555;">
        </view>
    </view>
</template>

<script>
// #ifdef APP-PLUS
var RewardAd = uni.requireNativePlugin('Gr-GoogleAdMod_RewardAd');
var InterstitialAd = uni.requireNativePlugin('Gr-GoogleAdMod_InterstitialAd');
var AndroidUtilModule = uni.requireNativePlugin('Gr-GoogleAdMod_AndroidUtilModule');
// #endif
export default {
    data() {
        return {
            rewardAdPlacementId: 'ca-app-pub-2669354798675201/9151244245', //激励广告测试Id
            interstitialAdPlacementId: 'ca-app-pub-2669354798675201/2851120276', //插屏广告测试Id
            hasLoadRewardAd: false, //是否已加载激励广告
            hasLoadInterstitialAd: false //是否已加载插屏广告
        };
    },
    onLoad() {
       
    },
    methods: {
      update_ad(){
        this.doLoadRewardAds(true)
      },
      doLoadRewardAds(is) {
        let that = this;
        // #ifdef APP
        InterstitialAd.loadInterstitialAd(that.interstitialAdPlacementId, ret => {
          if (ret.code == 201) {
            that.hasLoadInterstitialAd = true;
            uni.hideToast();
            if(is){
              that.doShowRewardAds()
            }
            console.log('加载成功.');
          } else {
            that.hasLoadInterstitialAd = false;
            console.log('加载失败.');
            uni.hideToast();
          }
        });
        // #endif
      },
      
      /**
       * 显示激励广告
       */
      doShowRewardAds() {
        let that = this;
        // #ifdef APP
        if (!that.hasLoadInterstitialAd) {
          that.doLoadRewardAds(true)
          return
        } else {
          InterstitialAd.showInterstitialAd(ret => {
              console.log(ret)
            that.hasLoadInterstitialAd = false;
            if (ret.code == 1006) {
              //开始显示广告
              uni.hideLoading();
            } else if (ret.code == 200) {
              //广告播放完成,关闭广告页面后执行
      
           
              that.hasLoadInterstitialAd = false;
            } else if (ret.code == 1008) {
              //广告未加载
              console.log('Please load the advertisement first.');
      
              uni.hideLoading();
            } else { //其他状态
      
              uni.hideLoading();
              console.log('error: ' + ret.msg);
            }
          });
        }
        // #endif
      }
      
    }
 
}
</script>

<style lang="scss">

</style>